//
//  HomeViewController.m
//  项目三
//
//  Created by rimi1 on 2016/12/9.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "HomeViewController.h"
#import "MenuViewController.h"
#import "LoreViewController.h"
#import "PersonalViewController.h"
#import "MenuSearchViewController.h"

#import "NBButton.h"

@interface HomeViewController ()

@property (nonatomic, strong) NBButton *nbButton;
@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, strong) UIViewController *main;
@property (nonatomic, strong) UIViewController *sub;

@property (nonatomic, strong) UIButton *maskButton;

@end

@implementation HomeViewController

- (void)viewWillAppear:(BOOL)animated {
    [self initHeadImg];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeAppearance];
}

#pragma mark - initialize

- (void)initializeAppearance {
    [self.navigationBar addSubview:self.nbButton];
    self.navigationBar.left.frame = CGRectMake(10, 25, 34, 34);
    self.navigationBar.left.layer.cornerRadius = self.navigationBar.left.bounds.size.width/2;
    self.navigationBar.left.layer.masksToBounds = YES;
    [self.navigationBar.right setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
    [self.view addSubview:self.mainScrollView];
    [self initializeVC];
}

- (void)initHeadImg {
    NSURL *url = [NSURL URLWithString:[MyUser sharedUser].headImage];
    UIImage *img = [UIImage imageNamed:@"个人"];
    [self.navigationBar.left.imageView sd_setImageWithURL:url placeholderImage:img completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            [self.navigationBar.left setImage:image forState:UIControlStateNormal];
        }else {
            [self.navigationBar.left setImage:img forState:UIControlStateNormal];
        }
    }];
}

- (void)initializeVC {
    self.main = [[MenuViewController alloc] init];
    [self addChildViewController:self.main];
    [self.mainScrollView addSubview:self.main.view];
    
    self.sub = [[LoreViewController alloc] init];
    CGRect sRect = self.sub.view.frame;
    sRect.origin.x = kWIDTH;
    self.sub.view.frame = sRect;
    [self addChildViewController:self.sub];
}

#pragma mark - respondsTo..

- (void)respondsToLeft:(UIButton *)sender {
    PersonalViewController *personal = [[PersonalViewController alloc] init];
    personal.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:personal animated:YES];
}

- (void)respondsToRight:(UIButton *)sender {
    MenuSearchViewController *search = [[MenuSearchViewController alloc] init];
    search.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:search animated:YES];
}
// 100：菜谱， 101：饮食知识
- (void)respondsToHead:(UIButton *)sender {
    sender.selected = YES;
    sender.backgroundColor = [UIColor whiteColor];
    
    if (sender.tag == 100) {
        self.nbButton.right.backgroundColor = [UIColor clearColor];
        self.nbButton.right.selected = NO;
        
        [self.mainScrollView addSubview:self.main.view];
        [self.mainScrollView setContentOffset:CGPointMake(0, 0)];
        [self.sub.view removeFromSuperview];
    }else {
        self.nbButton.left.backgroundColor = [UIColor clearColor];
        self.nbButton.left.selected = NO;
        
        [self.mainScrollView addSubview:self.sub.view];
        [self.mainScrollView setContentOffset:CGPointMake(kWIDTH, 0)];
        [self.main.view removeFromSuperview];
    }
    
}

#pragma mark - get

- (NBButton *)nbButton {
    
    if (!_nbButton) {
        
        _nbButton = [[NBButton alloc] initWithFrame:CGRectMake(0, 24, WXQAdaptionWidth(170), 30)];
        CGPoint point = _nbButton.center;
        point.x = kWIDTH/2;
        _nbButton.center = point;
        _nbButton.layer.cornerRadius = 5;
        [_nbButton.left setTitle:@"菜谱" forState:UIControlStateNormal];
        [_nbButton.right setTitle:@"饮食知识" forState:UIControlStateNormal];
        
        __weak HomeViewController *root = self;
        _nbButton.headBlock = ^(UIButton *button) {
            [root respondsToHead:button];
        };
        
    }
    return _nbButton;
}

- (UIScrollView *)mainScrollView {
    
    if (!_mainScrollView) {
        
        _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, kWIDTH, kHEIGHT - 64)];
        // 分页
        _mainScrollView.pagingEnabled = YES;
        // 滚动线
        _mainScrollView.showsVerticalScrollIndicator = NO;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        // 滚动内容范围
        _mainScrollView.contentSize = CGSizeMake(kWIDTH * 2, 0);
        _mainScrollView.scrollEnabled = NO;
        
    }
    return _mainScrollView;
}

@end












